package com.bestcem.xm.qdes.service.impl;

import com.alibaba.fastjson.JSON;
import com.bestcem.xm.component.mybatis.service.impl.XmServiceImpl;
import com.bestcem.xm.qdes.controller.vo.DeletedCountVo;
import com.bestcem.xm.qdes.entity.QdesTagGroup;
import com.bestcem.xm.qdes.mapper.QdesTagGroupMapper;
import com.bestcem.xm.qdes.mapper.QdesTagMapper;
import com.bestcem.xm.qdes.mapper.QdesTagRefMapper;
import com.bestcem.xm.qdes.service.QdesTagGroupService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class QdesTagGroupServiceImpl extends XmServiceImpl<QdesTagGroupMapper, QdesTagGroup> implements QdesTagGroupService {

    private @NonNull QdesTagMapper qdesTagMapper;
    private @NonNull QdesTagRefMapper qdesTagrefMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DeletedCountVo removeById(String id) {
        QdesTagGroup qdesTaggroup = baseMapper.selectById(id);

        //1.删除标签组下的标签
        String tagIds = qdesTaggroup.getTagIds();
        List<String> list = JSON.parseArray(tagIds, String.class);
        if (list.size() > 0) {
            qdesTagMapper.deleteBatchIds(list);
            //2.批量删除标签引用Tagref
            qdesTagrefMapper.deleteTagrefByTagIds(list);
        }
        //3.删除标签组
        DeletedCountVo deletedCountVo = new DeletedCountVo();
        int count = baseMapper.deleteById(id);
        if (count != 0) {
            deletedCountVo.setDeletedCount(1);
        } else {
            deletedCountVo.setDeletedCount(0);
        }
        return deletedCountVo;
    }

}
